package com.example.usharingtest.adapter;



import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.usharingtest.R;
import com.example.usharingtest.activities.ArticleViewActivity;
import com.example.usharingtest.activities.HomepageActivity;
import com.example.usharingtest.fragment.ArticleFragment;
import com.example.usharingtest.model.Article;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by yangchunying on 2017/8/26.
 */

public class postAdapter extends ArrayAdapter<Article> {
    private int resourceid;
    private boolean isZan =false;
    public postAdapter(Context con, int viewsourceid, List<Article> object)
    {
        super(con,viewsourceid,object);
        resourceid=viewsourceid;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final Article article =   getItem(position);
        View view= LayoutInflater.from(getContext()).inflate(resourceid,parent,false);
        TextView username=(TextView)view.findViewById(R.id.user_name);
        TextView userposttime=(TextView)view.findViewById(R.id.user_posttime);
        TextView userpostcontent=(TextView)view.findViewById(R.id.post_content);
        TextView zhuan=(TextView)view.findViewById(R.id.repost_total);
        TextView ping=(TextView)view.findViewById(R.id.comment_total);
        TextView zan=(TextView)view.findViewById(R.id.like_total);
        final ImageView zanimage = (ImageView)view.findViewById(R.id.like_image);
        ImageView pingimage = (ImageView)view.findViewById(R.id.comment_image);
        ImageView zhuanimage = (ImageView)view.findViewById(R.id.zhuan_image);
        ImageView image1=(ImageView)view.findViewById(R.id.user_headportrait);
        ImageView image2=(ImageView)view.findViewById(R.id.post_image);

        userpostcontent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO
                Intent intent = new Intent(getContext(), ArticleViewActivity.class);
                intent.putExtra("art",article);
                getContext().startActivity(intent);

            }
        });


        //点赞相应事件--未完成
        zanimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isZan){
                    isZan =false;
                    zanimage.setImageResource(R.drawable.market_icon_dislike);
                }else {
                    isZan =true;
                    zanimage.setImageResource(R.drawable.market_icon_liked);
                }

                zanimage.startAnimation(AnimationUtils.loadAnimation(
                        getContext(), R.anim.dianzan_anim));
            }

        });

        zan.setText(String.valueOf(article.getAgreenum()));

        //评论事件
        ping.setText(String.valueOf(article.getCommentnum()));
        pingimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 调用相应的事件
            }
        });
        //转发事件
        zhuan.setText(String.valueOf(article.getCollectnum()));
        zhuanimage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 调用相应的转发事件
            }
        });
        //文章内容填充
        username.setText(article.getUser().getUsername());
        //System.out.println(article.getContent());
        userposttime.setText(article.getPublishtime().toString());
        userpostcontent.setText(article.getContent());
        Picasso.with(view.getContext()).load(getContext().getResources().getString(R.string.URL_SERVER)+article.getUser().getUserphotopath()).into(image1);
        Picasso.with(view.getContext()).load(getContext().getResources().getString(R.string.URL_SERVER)+article.getPhotopath()).placeholder(R.drawable.loading).into(image2);

        return view;
    }

    @Override
    public void insert(Article object, int index) {
        super.insert(object, index);
    }

    public void addToTop(){

        System.out.println("上拉刷新成功！！！！！！");
    }

    public void addToBottom(int i){


        System.out.println("下拉加载成功！！！！！！");
    }
}
